import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'



Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:"/home"
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    // 路由元信息
    meta:{
      keep:true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/*路由懒加载 */ '../views/loginView.vue')
  },
  {
    path: '/sort',
    name: 'sort',
    component: () => import(/*路由懒加载 */ '../views/sortView.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import(/*路由懒加载 */ '../views/searchView.vue')
  },
  {
    path: '/shopCar',
    name: 'shopCar',
    component: () => import(/*路由懒加载 */ '../views/shopCarView.vue'),
    // 独享路由守卫
    // beforeEnter:(to,from,next)=>{
    //   let token = window.localStorage.getItem("token");
    //   // 判断是否登录
    //   if(!token){
    //     next("/login");
    //     return;
    //   }else{
    //     next();
    //   }
    // }
  },
  {
    path: '/productDetail',
    name: 'productDetail',
    component: () => import(/*路由懒加载 */ '../views/productDetailView.vue')
  },
  {
    path: '/threeDiscount',
    name: 'threeDiscount',
    component: () => import(/*路由懒加载 */ '../views/threeDiscountView.vue')
  },
  {
    path: '/getCode',
    name: 'getCode',
    component: () => import(/*路由懒加载 */ '../views/getCodeView.vue')
  }
  , {
    path: '/userInfo',
    name: 'userInfo',
    component: () => import(/*路由懒加载 */ '../views/userInfoView.vue')
  },
  {
    path: '/lightProduct',
    name: 'lightProduct',
    component: () => import(/*路由懒加载 */ '../views/lightProductView.vue')
  },
  {
    path: '/myCollect',
    name: 'myCollect',
    component: () => import(/*路由懒加载 */ '../views/myCollectView.vue')
  },
  {
    path: '/brandCollect',
    name: 'brandCollect',
    component: () => import(/*路由懒加载 */ '../views/brandCollectView.vue')
  },
  {
    path: '/payment',
    name: 'payment',
    component: () => import(/*路由懒加载 */ '../views/paymentView.vue')
  },
  {
    path: '/orderList',
    name: 'orderList',
    component: () => import(/*路由懒加载 */ '../views/orderListView.vue')
  },
  {
    path: '/myAddress',
    name: 'myAddress',
    component: () => import(/*路由懒加载 */ '../views/myAddressView.vue')
  },
  {
    path: '/addMyAddress',
    name: 'addMyAddress',
    component: () => import(/*路由懒加载 */ '../views/addMyAddressView.vue')
  },
  {
    path: '/modifyAddress',
    name: 'modifyAddress',
    component: () => import(/*路由懒加载 */ '../views/modifyAddressView.vue')
  },
  {
    path: '/brand',
    name: 'brand',
    component: () => import(/*路由懒加载 */ '../views/brandView.vue')
  }, 
  {
    path: '/searchList',
    name: 'searchList',
    component: () => import(/*路由懒加载 */ '../views/searchListView.vue')
  },
  {
    path: '/waitList',
    name: 'waitList',
    component: () => import(/*路由懒加载 */ '../views/waitListView.vue')
  },
]



// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location){
  return originalPush.call(this,location).catch(err=>err);
}

//  解决触发导航守卫问题
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location, onResolve, onReject) {
//   if (onResolve || onReject) {
//     return originalPush.call(this, location, onResolve, onReject)
//   }
//   return originalPush.call(this, location).catch(err => err)

// }

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
